package top.chukongxiang.project.base.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.ToStringSerializer;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.http.MediaType;

import java.util.ArrayList;
import java.util.List;

public class FastJsonConfiguration {

    public static final List<String> HttpSchemes = new ArrayList<>();
    static {
        HttpSchemes.add("HTTP");
        HttpSchemes.add("HTTPS");
    }

    private static FastJsonHttpMessageConverter fastJsonHttpMessageConverter = null;

    private static SerializerFeature[] fastJsonSerializerFeature = null;

    public static FastJsonHttpMessageConverter getFastJsonHttpMessageConverter(){
        if (fastJsonHttpMessageConverter!=null) {
            return fastJsonHttpMessageConverter;
        }
        fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();

        //升级最新版本需加=============================================================
        List<MediaType> supportedMediaTypes = new ArrayList<>();
        supportedMediaTypes.add(MediaType.APPLICATION_JSON);
        supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        supportedMediaTypes.add(MediaType.APPLICATION_ATOM_XML);
        supportedMediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED);
        // 支持的转换请求类型，不转换字节流
        // supportedMediaTypes.add(MediaType.APPLICATION_OCTET_STREAM);
        supportedMediaTypes.add(MediaType.APPLICATION_PDF);
        supportedMediaTypes.add(MediaType.APPLICATION_RSS_XML);
        supportedMediaTypes.add(MediaType.APPLICATION_XHTML_XML);
        supportedMediaTypes.add(MediaType.APPLICATION_XML);
        supportedMediaTypes.add(MediaType.IMAGE_GIF);
        supportedMediaTypes.add(MediaType.IMAGE_JPEG);
        supportedMediaTypes.add(MediaType.IMAGE_PNG);
        supportedMediaTypes.add(MediaType.TEXT_EVENT_STREAM);
        supportedMediaTypes.add(MediaType.TEXT_HTML);
        supportedMediaTypes.add(MediaType.TEXT_MARKDOWN);
        supportedMediaTypes.add(MediaType.TEXT_PLAIN);
        supportedMediaTypes.add(MediaType.TEXT_XML);
        fastJsonHttpMessageConverter.setSupportedMediaTypes(supportedMediaTypes);

        //创建配置类
        com.alibaba.fastjson.support.config.FastJsonConfig fastJsonConfig = new com.alibaba.fastjson.support.config.FastJsonConfig();

        SerializeConfig serializeConfig = SerializeConfig.globalInstance;
        //long转为string
        serializeConfig.put(Long.class , ToStringSerializer.instance);
        serializeConfig.put(Long.TYPE , ToStringSerializer.instance);
        // serializeConfig.put(ServiceTypeEnum.class, new ServiceTypeEnumSerializer());
        fastJsonConfig.setSerializeConfig(serializeConfig);
        fastJsonConfig.setSerializerFeatures(getFastJsonSerializerFeature());
        fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
        return fastJsonHttpMessageConverter;
    }

    public static SerializerFeature[] getFastJsonSerializerFeature(){
        if (fastJsonSerializerFeature == null) {
            fastJsonSerializerFeature = new SerializerFeature[] {
                    // 是否输出值为null的字段,默认为false
                    //SerializerFeature.WriteMapNullValue,

                    // SerializerFeature.BrowserCompatible,
                    SerializerFeature.WriteNullListAsEmpty,

                    // 将Collection类型字段的字段空值输出为[]
                    SerializerFeature.WriteDateUseDateFormat,

                    // 禁用循环引用
                    SerializerFeature.DisableCircularReferenceDetect,

                    // SerializerFeature.WriteNullBooleanAsFalse,

                    // SerializerFeature.WriteNullNumberAsZero,

//                    SerializerFeature.SortField,

                    // SerializerFeature.PrettyFormat,
            };
        }

        return fastJsonSerializerFeature;
    }

    public static void config() {
        // 序列化对象时添加 @type
        JSON.DEFAULT_GENERATE_FEATURE |= SerializerFeature.WriteClassName.getMask();

        // 反序列化时先判断是否包含 @type，如果包含，直接根据@type序列化
        JSON.DEFAULT_PARSER_FEATURE |= Feature.SupportAutoType.getMask();
    }


}
